<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\middleware\Auth;
use app\model\OrdersModel;
use app\model\ProductsModel;
use app\model\ShopsModel;
use app\model\StatisticsModel;
use think\App;

class Index extends Base {

    protected $middleware = [Auth::class];

    private $aid;

    private $shopsModel;

    private $productsModel;

    private $req;



    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->shop_id = isset($this->req['shop_id']) ? $this->req['shop_id'] : "";

        $this->shopsModel = new ShopsModel();

        $this->aid = $this->isLogin();

        $this->shopIds = $this->shopsModel->getShopIds($this->aid);

        $this->statisticsModel = new StatisticsModel();

        $this->ordersModel = new OrdersModel();

        $this->productsModel = new ProductsModel();


    }


    public function statistics()
    {

        $data =[];

        $data["pv"] = $this->statisticsModel->getPv() ? $this->statisticsModel->getPv() : 0;

        $data["uv"] = $this->statisticsModel->getUv() ? $this->statisticsModel->getUv() : 0;

        $data["todayLoginNum"] = $this->statisticsModel->getTodayLogin() ? $this->statisticsModel->getTodayLogin() : 0;

        $data["todayRegisterNum"] = $this->statisticsModel->getTodayRegister() ? $this->statisticsModel->getTodayRegister() : 0;

        $data["todayTradeNum"] = $this->ordersModel->getTradeNum($this->shopIds);

        $todayTradeTotalAmoun =  $this->ordersModel->getTradeTotalAmount($this->shopIds);
        $data["todayTradeTotalAmoun"] = $todayTradeTotalAmoun ? round(  $todayTradeTotalAmoun / 100,2) : 0;

        $data["unSendNum"] = $this->ordersModel->getUnSendNum($this->shopIds);

        $data["unHandelAftersalesNum"] = $this->ordersModel->getUnHandelAftersalesNum($this->shopIds);

        //获取商品数量
        $where=[];
        $where[] = ['p.is_delete',"=",0];
        $where[] = ['p.shop_id','in',$this->shopIds];

        $data["productNum"] = $this->productsModel->getTotal($where);

        $data["shopsNum"] = count($this->shopIds);

        $data["lastTime"] = date("Y-m-d H:i:s");

        return $this->succ($data);

    }
}
